package com.zhihaoliang.coroutine.buss.flow.func

import com.zhihaoliang.coroutine.util.log
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

/**
 * @author 支豪亮
 * @date 2022/10/24 16:17
 * @des Since simple().collect is called from the main thread, the body of simple's flow is also called in the main thread
 */
object FlowThread {

    fun onThread(mainScope: CoroutineScope) = mainScope.launch {
        simple().collect { value -> log("${Thread.currentThread()} :Collected $value") }
    }

    private fun simple(): Flow<Int> = flow {
        log("${Thread.currentThread()}:Started simple flow")
        for (i in 1..3) {
            emit(i)
        }
    }
}